package com.eryos.android.cigarettecounter;

/**
 * Project :  CigaretteCounter
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/CigaretteCounter/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.eryos.android.cigarettecounter.beans.SmokeTime;
import com.eryos.android.cigarettecounter.io.Preferences;


public class CigaretteCounter extends RootActivity {


    /** *************** Instance Variables *************** **/	
	private String monthValueLabel ;
    private String weekValueLabel ;

    private String delayValueLabel ;
    private String lastValueLabel ;

    private float cigarettePrice = 0;
    private Integer  val = 0;

    /** *************** UI Elements *************** **/
    private TextView dailyCountValue;
    private TextView delayValue;
    private TextView lastCigValue;
    private TextView avgWeekValue;
    private TextView avgMonthValue;
	
    private Button cigButton; 

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tracker.trackPageView("Home");
        
        // Listeners & Handlers 
        cigButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	db.open();
            	clickAddButton();
            }
        });
    }
    
    /**
     * init Values ( not UI ) for this activiry
     */
    public void initValues(){
    	monthValueLabel = new String(avgMonthValue.getText()+"");
    	weekValueLabel = new String(avgWeekValue.getText()+"");
    	delayValueLabel = new String(delayValue.getText()+"");
    	lastValueLabel = new String(lastCigValue.getText()+"");

        showDetails();
    }
    
    public void clickAddButton(){
    	db.insertCurrentTime("", cigarettePrice);

    	showDetails();
    	Toast.makeText(this, R.string.home_insert_OK, Toast.LENGTH_SHORT).show();
    }
    
    public void showDetails(){
    	Preferences pref = new Preferences(getSharedPreferences(PREFS_NAME, 0));
    	pref.readParamFromFile();
    	cigarettePrice = pref.getCigarettePrice();

    	// Daily counter
    	val = db.getDailyCount();
    	dailyCountValue.setText("     "+val+" cigarette(s) : ");

    	// Last cigarette
    	SmokeTime last = db.getDelaySinceLast();
    	if ( lastValueLabel != null && last != null && last.getTotalMinutes() < 0 )
    		lastCigValue.setText("     NA");
    	else
    		lastCigValue.setText("     "+lastValueLabel.replaceAll("#1", last.getHour()+"").replaceAll("#2", last.getMinute()+"").replaceAll("#3", last.getTotalMinutes()+""));
    	
    	// Weekly average 
    	double weekAvg = db.getWeeklyAverage();
    	if ( weekAvg <= 0 )
    		avgWeekValue.setText("     NA");
    	else
    	avgWeekValue.setText("     "+weekValueLabel.replaceAll("#", weekAvg+""));
    	
    	// Monthly average
    	double monthAvg = db.getMonthlyAverage();
    	if ( monthAvg <= 0 )
    		avgMonthValue.setText("     NA");
    	else
    		avgMonthValue.setText("     "+monthValueLabel.replaceAll("#", monthAvg+""));
    	
    	// Delay between 2 cigarettes 
    	SmokeTime delay = db.getAverageDelay();
    	if ( delayValueLabel != null && delay != null && delay.getTotalMinutes() < 0 )
    		delayValue.setText("     NA");
    	else
    		delayValue.setText("     "+delayValueLabel.replaceAll("#1", delay.getHour()+"").replaceAll("#2", delay.getMinute()+"").replaceAll("#3", delay.getTotalMinutes()+""));

    	hideWaitingMessage();
    }
    
    /**
     * Init all UI elements
     */
    public void initUI(){
        setContentView(R.layout.main);

        showWaitingMessage();

        cigButton = (Button) findViewById(R.id.Button01);
         
        dailyCountValue = (TextView) findViewById(R.id.cig_today_value);
        delayValue 		= (TextView) findViewById(R.id.delay_value);
        lastCigValue 	= (TextView) findViewById(R.id.last_cig_value);
        avgWeekValue	= (TextView) findViewById(R.id.avg_week_value);
        avgMonthValue	= (TextView) findViewById(R.id.avg_month_value);
    }
    
}
